/**
 * 阶梯租金编辑后事件
 */
 import moment from 'moment';
 import constants from '../../constants';
 
 
 const { AREA: { CARD: { STEPRENTSTANDARD_TAB: steprentstandard_tab, BASIC_INFO_AREA: basicInfoForm } } } = constants;
 
 export default function(props, moduleId, field, newValue, changedrows, index, record, type, method) {
     const { form, cardTable } = props;
     //租赁负债相关的字段进行修改后需要重新计算租赁负债
     this.isNeedCalculateLeaseLiabilities = true;
 
     if ('rent' === field) {
         if (newValue) {
             const rows = cardTable.getNumberOfRows(steprentstandard_tab);
             if (rows == index + 1) {
                 cardTable.addRow(steprentstandard_tab);
             }
             let lastEndDate = cardTable.getValByKeyAndIndex(steprentstandard_tab, index, 'end_date').value;
             if(lastEndDate) {
                 let nextDate = moment(lastEndDate, 'YYYY-MM-DD HH:mm:ss').add(1, 'd').format('YYYY-MM-DD HH:mm:ss');
                 cardTable.setValByKeyAndIndex(steprentstandard_tab, index + 1, 'start_date', { value: nextDate });
             }
         }
     }
 
     /* 开始月份与结束月份校验 */
     if ([ 'start_date', 'end_date' ].includes(field)) {
         let start_date = cardTable.getValByKeyAndIndex(steprentstandard_tab, index, 'start_date').value;
         let end_date = cardTable.getValByKeyAndIndex(steprentstandard_tab, index, 'end_date').value;
         if (start_date && end_date) {
             let start = moment(start_date, 'YYYY-MM-DD');
             let end = moment(end_date, 'YYYY-MM-DD');
             if (start.isAfter(end)) {
                 /* "201201504A-0000105": "阶梯租金结束月份不能早于开始月份！" */
                 showMessage(props, { content: getMultiLangByID('201201504A-0000105'), color: 'warning' });
                 if (field === 'start_date') {
                     cardTable.setValByKeyAndIndex(steprentstandard_tab, index, 'start_date', { value: '' });
                 }
                 if (field === 'end_date') {
                     cardTable.setValByKeyAndIndex(steprentstandard_tab, index, 'end_date', { value: '' });
                 }
                 return false;
             }
         }
     }
 
     /* 开始月份 */
     if ('start_date' === field) {
         if (newValue) {
             /* 按阶梯租金计租 */
             let step_lease_flag = form.getFormItemsValue(basicInfoForm, 'step_lease_flag').value;
             if (step_lease_flag === true) {
                 /* 同步表头的起算日期，满足免租期要求 */
                 if (index === 0) {
                     form.setFormItemsValue(basicInfoForm, {
                         /* 起算日期 */
                         starting_date: { value: newValue }
                     });
                 }
             }
         }
     }
 
     /* 结束月份 */
     if ('end_date' === field) {
         let rent = cardTable.getValByKeyAndIndex(steprentstandard_tab, index, 'rent').value;
         const rows = cardTable.getNumberOfRows(steprentstandard_tab);
         if (newValue && rent) {
             if (rows === index + 1) {
                 cardTable.addRow(steprentstandard_tab);
             }
             let nextDate = moment(newValue, 'YYYY-MM-DD HH:mm:ss').add(1, 'd').format('YYYY-MM-DD HH:mm:ss');
             cardTable.setValByKeyAndIndex(steprentstandard_tab, index + 1, 'start_date', { value: nextDate });
         }
 
         if (newValue) {
             /* 按阶梯租金计租 */
             let step_lease_flag = form.getFormItemsValue(basicInfoForm, 'step_lease_flag').value;
             if (step_lease_flag === true) {
                 /* 同步表头的止租日期 */
                 if (rows === index + 1) {
                     // 续租选择权
                     let renewal_flag = form.getFormItemsValue(basicInfoForm, "renewal_flag").value;
                     if (renewal_flag === true){
                         form.setFormItemsValue(basicInfoForm, {
                             /* 续租止租日期 */
                             relet_ending_date: { value: newValue }
                         });
                     }else {
                         form.setFormItemsValue(basicInfoForm, {
                             /* 止租日期 */
                             ending_date: { value: newValue }
                         });
                     }
                 }
             }
         }
     }
 
 }
 
